package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.catalina.valves.ExtendedAccessLogValve;
import org.springframework.stereotype.Repository;

import dao.PersonDao;
import po.Person;
@Repository(value="personDao")
public class PersonDaoImpl implements PersonDao{
	
	@PersistenceContext(name="factory")
	private EntityManager manager;
	
	@Override
	public Person addPerson(Person person) {
		person.setId(null);
		manager.persist(person);
		return person;
	}

	@Override
	public int deletePerson(String id) {
		Person person = manager.find(Person.class, id);
		manager.remove(person);
		return 1;
	}

	@Override
	public Person updatePerson(Person person) {
		Person ps = manager.find(Person.class, person.getId());
		ps.setName(person.getName());
		ps.setSal(person.getSal());
		ps.setSex(person.isSex());
		ps.setBirth(person.getBirth());
		manager.merge(ps);
		return ps;
	}

	@Override
	public List<Person> findAllPerson() {
		Query query = manager.createQuery("from Person");
		return  query.getResultList();
	}

}
